package com.warehouse.service;

import com.warehouse.dao.CargotypeDao;
import com.warehouse.dao.impl.CargotypeDapImpl;
import com.warehouse.entity.Cargo;
import com.warehouse.entity.Cargotype;
import com.warehouse.utils.Page;

import java.util.List;

public class CargotypeService {

    private CargotypeDao cargotypeDao=new CargotypeDapImpl();
    public Page<Cargotype> queryPage(int start, int pageSize) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = cargotypeDao.count();//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<Cargotype> list = cargotypeDao.allPage(startPage, pageSize);
        return new Page<Cargotype>(start, totalPage, pageSize, list);
    }

    public int add(Cargotype cargotype) {
        return cargotypeDao.add(cargotype);
    }

    public int update(Cargotype Cargotype) {
        return cargotypeDao.update(Cargotype);
    }


    public int delete(int id) {
        return cargotypeDao.delete(id);
    }

    public Cargotype byId(int id) {
        return cargotypeDao.byId(id);
    }

    public Page<Cargotype> byCardNum(int start, int pageSize, String name) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = cargotypeDao.countByAdminNum(name);//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<Cargotype> cargos = cargotypeDao.byAdminNum(startPage, pageSize, name);
        return new Page<Cargotype>(start, totalPage, pageSize, cargos);
    }

    public List<Cargotype> all(){
        return cargotypeDao.all();
    }
}
